import BigNumber from "bignumber.js";

export function formartMoney(s: string, digit: number): string {
  let b =(new BigNumber((new BigNumber(s)).toFixed(digit, BigNumber.ROUND_DOWN))).toString()
  // let b = new BigNumber(s).toFixed(digit, 1);
  // console.log(b)
  if(b.toString()=="NaN")
  {
    // console.log(s)
    b="0";
    return b;
  }
  // for (let i = 1; i < 4; i++) {
  //   if (b[b.length - i] !== '0') {
  //     return b.substring(0, b.length - i + 1);
  //   }
  // }

  // return b.substring(0, b.length - 3);
  return b.toString();

}

export function timeFormat(t: number) {
  // var z = new Date(t);

  // let M: any = z.getMonth() + 1;
  // if (M < 10) {
  //   M = `0${M}`;
  // }
  // let d: any = z.getDay();
  // if (d < 10) {
  //   d = `0${d}`;
  // }
  // let h: any = z.getHours();
  // if (h < 10) {
  //   h = `0${h}`;
  // }
  // let m: any = z.getMinutes();
  // if (m < 10) {
  //   m = `0${m}`;
  // }
  // let s: any = z.getSeconds();
  // if (s < 10) {
  //   s = `0${s}`;
  // }
  // return `${z.getFullYear()}-${M}-${d} ${h}:${m}:${s}`;

  var date = new Date(t);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var year = date.getFullYear(),
      month = ("0" + (date.getMonth() + 1)).slice(-2),
      sdate = ("0" + date.getDate()).slice(-2),
      hour = ("0" + date.getHours()).slice(-2),
      minute = ("0" + date.getMinutes()).slice(-2),
      second = ("0" + date.getSeconds()).slice(-2);
  return year + "-" + month + "-" + sdate + " " + hour + ":" + minute + ":" + second;
}

export function toDecimal2(x:string) {
  var fx = parseFloat(x);
  if (isNaN(fx)) {
    return false;
  }
  var f = Math.round(fx*100)/100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
}

export function sleep (time:number) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

